package study.distributed.transaction.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.distributed.transaction.dao.transaction0.OrderMapper;
import study.distributed.transaction.dao.transaction1.OrderItemMapper;
import study.distributed.transaction.model.OrderItemModel;
import study.distributed.transaction.model.OrderModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;



    @Transactional(rollbackFor = Exception.class)
    public void insertListWithTransaction() {
        for (int i = 1; i < 5; i++) {
            OrderModel orderModel = new OrderModel();
            orderModel.setCreateTime(new Date());
            orderModel.setUserId(11);
            orderMapper.insert(orderModel);
            log.info("orderId:"+orderModel.getOrderId());

            List<OrderItemModel> itemModels = new ArrayList<OrderItemModel>();
            for (int j = 1; j < 5; j++) {
                OrderItemModel itemModel = new OrderItemModel();
                itemModel.setOrderId(orderModel.getOrderId());
                itemModel.setName("商品"+j);
                itemModels.add(itemModel);
            }
            orderItemMapper.insertList(itemModels);
        }
        int i = 1/0;
    }
}
